package com.rpay.service.question.impl;

import com.rpay.common.dto.question.QuestionDTO;
import com.rpay.dao.question.QuestionDao;
import com.rpay.model.question.AnswerDO;
import com.rpay.model.question.QuestionDO;
import com.rpay.common.dto.question.QuestionQueryDTO;
import com.rpay.model.question.QuestionQueryDO;
import com.rpay.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionDao questionDao;

    @Override
    public QuestionQueryDTO getDataList(QuestionQueryDTO questionQueryDTO, Integer page, Integer pageSize) {
        QuestionQueryDO questionQueryDO = convertSqlDTOToDO(questionQueryDTO);
        List<QuestionDO> questionDOList = questionDao.selectDataList(questionQueryDO);
        List<QuestionDTO> list = new ArrayList<>();
        if (questionDOList != null && questionDOList.size() > 0) {
            for (QuestionDO questionDO : questionDOList) {
                QuestionDTO questionDTO = convertDOToDTO(questionDO);
                list.add(questionDTO);
            }
        }
        Integer count = questionDao.selectCount(questionQueryDO);
        QuestionQueryDTO data = convertSqlDOToDTO(list, page, pageSize, count);
        return data;
    }

    @Override
    public QuestionDTO getDetail(QuestionDTO questionDTO) {
        QuestionDO query = convertDTOToDO(questionDTO);
        QuestionDO questionDO = questionDao.findById(query);
        QuestionDTO detail = convertDOToDTO(questionDO);
        return detail;
    }

    @Override
    public boolean saveQuestion(QuestionDTO questionDTO) {
        QuestionDO questionDO = convertDTOToDO(questionDTO);
        questionDao.insert(questionDO);
        int id = questionDO.getId();
        AnswerDO answerDO = convertAnswerDTOToDO(questionDTO, id);
        questionDao.insertAnswer(answerDO);
        return true;
    }

    @Override
    public boolean deleteQuestion(Integer id) {
        questionDao.deleteById(id);
        questionDao.deleteAnswerById(id);
        return true;
    }

    @Override
    public boolean updateQuestion(QuestionDTO questionDTO) {
        QuestionDO questionDO = convertDTOToDO(questionDTO);
        if (questionDO.getQuestion() != null || questionDO.getQuestionType() != null || questionDO.getVisible() != null || questionDO.getQuestionType() != null) {
            questionDao.updateById(questionDO);
        }
        if (questionDO.getAnswer() != null || questionDO.getEnAnswer() != null) {
            questionDao.updateAnswerById(questionDO);
        }
        return true;
    }

    private QuestionQueryDO convertSqlDTOToDO(QuestionQueryDTO questionQueryDTO) {
        QuestionQueryDO questionQueryDO = null;
        if (questionQueryDTO != null) {
            questionQueryDO = new QuestionQueryDO();
            questionQueryDO.setVisible(questionQueryDTO.getVisible());
            questionQueryDO.setQuestionType(questionQueryDTO.getQuestionType());
            questionQueryDO.setKeyword(questionQueryDTO.getKeyword());
            questionQueryDO.setPage(questionQueryDTO.getPage());
            questionQueryDO.setPageSize(questionQueryDTO.getPageSize());
        }
        return questionQueryDO;
    }

    private QuestionDTO convertDOToDTO(QuestionDO questionDO) {
        QuestionDTO questionDTO = null;
        if (questionDO != null) {
            questionDTO = new QuestionDTO();
            questionDTO.setId(questionDO.getId());
            questionDTO.setVisible(questionDO.getVisible());
            questionDTO.setUsername(questionDO.getUsername());
            questionDTO.setQuestion(questionDO.getQuestion());
            questionDTO.setQuestionType(questionDO.getQuestionType());
            questionDTO.setAnswer(questionDO.getAnswer());
            questionDTO.setEnQuestion(questionDO.getEnQuestion());
            questionDTO.setEnAnswer(questionDO.getEnAnswer());
            questionDTO.setCreateTime(questionDO.getCreateTime());
            questionDTO.setUpdateTime(questionDO.getUpdateTime());
        }
        return questionDTO;
    }

    private QuestionDO convertDTOToDO(QuestionDTO questionDTO) {
        QuestionDO questionDO = null;
        if (questionDTO != null) {
            questionDO = new QuestionDO();
            questionDO.setId(questionDTO.getId());
            questionDO.setVisible(questionDTO.getVisible());
            questionDO.setQuestionType(questionDTO.getQuestionType());
            questionDO.setUserId(questionDTO.getUserId());
            questionDO.setQuestion(questionDTO.getQuestion());
            questionDO.setEnQuestion(questionDTO.getEnQuestion());
            questionDO.setEnAnswer(questionDTO.getEnAnswer());
            questionDO.setAnswer(questionDTO.getAnswer());
        }
        return questionDO;
    }

    private AnswerDO convertAnswerDTOToDO(QuestionDTO questionDTO, Integer id) {
        AnswerDO answerDO = null;
        if (questionDTO != null) {
            answerDO = new AnswerDO();
            answerDO.setCreateUid(questionDTO.getUserId());
            answerDO.setUpdateUid(questionDTO.getUserId());
            answerDO.setAnswer(questionDTO.getAnswer());
            answerDO.setEnAnswer(questionDTO.getEnAnswer());
            answerDO.setQuestionId(id);
        }
        return answerDO;
    }

    private QuestionQueryDTO convertSqlDOToDTO(List list, Integer page, Integer pageSize, Integer count) {
        QuestionQueryDTO questionQueryDTO = null;
        questionQueryDTO = new QuestionQueryDTO();
        questionQueryDTO.setList(list);
        questionQueryDTO.setPage(page);
        questionQueryDTO.setPageSize(pageSize);
        questionQueryDTO.setTotalRow(count);
        return questionQueryDTO;
    }
}
